package test;

import java.util.Date;

public class Demo2 {

    public static int count;

    public static final Object object = new Object();

    static void add() {
        count += 2;
    }

    static void subtract() {
        count -= 2;
    }

    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(() -> {
            synchronized (object) {
                add();
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (object) {
                subtract();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("count: " + count);
    }
}
